/* Includes ------------------------------------------------------------------*/
#include "comp.h"
#include "global.h"
#include "var.h"
#include "TimeEvent.h"
#include "display.h"
/*********************************************************************
 * @fn      void SensorErrCompressorRun_Evt( void )
 *
 * @brief   柜温传感器故障下压缩机运行
 *
 * @param   none
 *
 * @return  none
 */
static void SensorErrCompressorRun_Evt(void)
{
    if (Flag2.OneBit.SensorErr == 1) // 传感器故障下，压缩机运行
    {
        if (CompressorState == OFF) // 关机状态下
        {
            if ((tPara.CompressorDownTimeCount * CompTaskCycle) >= (cfgPara[CoolingMode].sensorErrCompStopTime * tMinute) || Flag2.OneBit.CompFirstRunStart == 1) // 停机时间大于最小停机时间
            {
                Flag2.OneBit.CompFirstRunStart = 0;
                Reg_CompressorState(REG_ON); // 压缩机开启
                // CompWorkState = Running;     // 制冷中
                Flag2.OneBit.SensorErr = 1;
            }
            else // 停机时间不够,继续停机
            {
                Reg_CompressorState(REG_OFF); // 压缩机关闭
                // CompWorkState = Stopping;     //
            }
        }
        else // 运行状态下
        {
            Reg_CompressorState(REG_ON);
            // CompWorkState = Running;

            tPara.sensorErrCompRunTimeCount++;
            if ((tPara.sensorErrCompRunTimeCount * CompTaskCycle) >= (cfgPara[CoolingMode].sensorErrCompRunTime * tMinute))
            {
                tPara.sensorErrCompStopTimeCount = 0;
                tPara.sensorErrCompRunTimeCount = 0;

                Reg_CompressorState(REG_OFF); // 压缩机关闭
                // CompWorkState = Stopping;     //
                Flag2.OneBit.SensorErr = 0;
            }
        }
    }
    else // 传感器故障下，压缩机停机
    {
        Reg_CompressorState(REG_OFF); // 压缩机关闭
        // CompWorkState = Stopping;

        tPara.sensorErrCompStopTimeCount++;
        if ((tPara.sensorErrCompStopTimeCount * CompTaskCycle) >= (cfgPara[CoolingMode].sensorErrCompStopTime * tMinute))
        {
            tPara.sensorErrCompStopTimeCount = 0;
            tPara.sensorErrCompRunTimeCount = 0;
            Flag2.OneBit.SensorErr = 1;

            Reg_CompressorState(REG_ON); // 压缩机开启
            // CompWorkState = Running;     // 制冷中
        }
    }
}

/*********************************************************************
 * @fn      void SensorErrCompressorRun_Evt( void )
 *
 * @brief   柜温传感器故障下压缩机运行
 *
 * @param   none
 *
 * @return  none
 */
static void SensorNormalCompressorRun_Evt(void)
{
    CtrTargetTemp1 = cfgPara[CoolingMode].St * 10 + cfgPara[CoolingMode].rd1; // 上回差控制
    CtrTargetTemp2 = cfgPara[CoolingMode].St * 10 - cfgPara[CoolingMode].rd2; // 下回差控制

    if (Flag2.OneBit.CompFirstRun == 0)
    {
        CompRunMaxTime = cfgPara[CoolingMode].CompRunTimeMax * tHour;
    }
    else
    {
        CompRunMaxTime = cfgPara[CoolingMode].CompFirstRunTimeMax * tHour;
    }

    // 清除传感器故障压缩机运行事件
    Flag2.OneBit.SensorErr = 1;

    // 传感器正常情况, 压缩机关机逻辑
    if (CompressorState == ON) //&&  //CompWorkState == Running) //运行状态下检测关机
    {
        // 关机条件（温度和最大开机时间）
        if (cfgPara[CoolingMode].CompRunTimeMax != 0) // 压缩机最大运行时间不为0,最大时间到要关机
        {
            if (TempInside < CtrTargetTemp2 || (tPara.CompressorRunTimeCount * CompTaskCycle) >= CompRunMaxTime) // 温度低于设定值, 或者压缩机运行时间超过最大开机时间
            {
                Flag2.OneBit.CompFirstRun = 0;

                // CompWorkState = Stopping;
                Reg_CompressorState(REG_OFF);

                if (Flag3.OneBit.factoryMode == 1) // 进入工厂测试逻辑下
                {
                    tPara.factoryModeTimeCount = (cfgPara[CoolingMode].FactoryTestTime * tMinute) / CompTaskCycle;
                    // Flag3.OneBit.factoryModePart = 0; // 第一阶段
                }
            }
            else
            {
                Reg_CompressorState(REG_ON);
                // CompWorkState = Running;
            }
        }
        else // 压缩机最大运行时间为0，不需要最大时间0
        {
            if (TempInside < CtrTargetTemp2) // 温度低于设定值, 或者压缩机运行时间超过最大开机时间
            {
                Flag2.OneBit.CompFirstRun = 0;

                // CompWorkState = Stopping;
                Reg_CompressorState(REG_OFF);
                if (Flag3.OneBit.factoryMode == 1) // 进入工厂测试逻辑下
                {
                    tPara.factoryModeTimeCount = (cfgPara[CoolingMode].FactoryTestTime * tMinute) / CompTaskCycle;
                    // Flag3.OneBit.factoryModePart = 0; // 第一阶段
                }
            }
            else
            {
                Reg_CompressorState(REG_ON);
                // CompWorkState = Running;
            }
        }
    }
    else if (CompressorState == OFF) // 压缩机关机的状态下，检测压缩机运行逻辑
    {
        if (TempInside > CtrTargetTemp1) // 温度大于(设定+回差) 且 停机时间大于最小停机时间
        {
            if (Flag2.OneBit.CompFirstRunStart == 1) // 第一次延时后无强制运行启动
            {
                Flag2.OneBit.CompFirstRunStart = 0;
                Reg_CompressorState(REG_ON);
                // CompWorkState = Running;
            }
            else
            {
                if ((tPara.CompressorDownTimeCount * CompTaskCycle) >= (cfgPara[CoolingMode].CompDownTimeMin * tMinute))
                {
                    Reg_CompressorState(REG_ON);
                    // CompWorkState = Running;
                }
                else
                {
                    Reg_CompressorState(REG_OFF); ////制冷停止中
                    // CompWorkState = Ready;        // 制冷延时
                }
            }
        }
        else
        {
            // CompWorkState = Stopping;
            Reg_CompressorState(REG_OFF); ////制冷停止中
        }
    }
}

/*********************************************************************
 * @fn      void QuickFrozenModeCompRun_Evt( void )
 *
 * @brief   速冻模式下压缩机运行
 *
 * @param   none
 *
 * @return  none
 */
static void QuickFrozenModeCompRun_Evt(void)
{
    // 清除传感器故障压缩机运行事件
    Flag2.OneBit.SensorErr = 1;

    // 传感器正常情况, 压缩机关机逻辑
    if (CompressorState == ON) //&&  //CompWorkState == Running) //运行状态下检测关机
    {
        Reg_CompressorState(REG_ON);
        // CompWorkState = Running;

        if (ErrorState.OneBit.Ntc0Error == 0) // 柜温传感器故障
        {
            CtrTargetTemp2 = cfgPara[CoolingMode].St * 10 - cfgPara[CoolingMode].rd2 - cfgPara[CoolingMode].quickFrozenTemp * 10;
            if (TempInside <= CtrTargetTemp2) // 温度低于设定值, 或者压缩机运行时间超过最大开机时间
            {
                tPara.quickFrozenTimeCount = 0;
                QuickFrozenState = q_Idle; // 退出速冻模式
            }
        }

        tPara.quickFrozenTimeCount++;
        if ((tPara.quickFrozenTimeCount * CompTaskCycle) >= (cfgPara[CoolingMode].quickFrozenTime * tHour)) // 温度低于设定值, 或者压缩机运行时间超过最大开机时间
        {
            tPara.quickFrozenTimeCount = 0;
            QuickFrozenState = q_Idle; // 退出速冻模式
        }
    }
    else if (CompressorState == OFF) // 压缩机关机的状态下，检测压缩机运行逻辑
    {
        if ((tPara.CompressorDownTimeCount * CompTaskCycle) >= (cfgPara[CoolingMode].CompDownTimeMin * tMinute)) // 停机时间大于最小停机时间
        {
            Reg_CompressorState(REG_ON);
            // CompWorkState = Running;

            tPara.CompressorRunTimeCount = 0; // 制冷中
            tPara.quickFrozenTimeCount = 0;
        }
        else
        {
            // CompWorkState = Ready;        // 制冷延时
            Reg_CompressorState(REG_OFF); ////制冷停止中
            tPara.quickFrozenTimeCount = 0;
        }
    }
}

/*********************************************************************
 * @fn      void CompressorControlEvt (void)
 *
 * @brief   压缩机控制事件
 *
 * @param   none
 *
 * @return  none
 */
void CompressorControlEvt(void)
{
    timer_10msDB[CompCtr_TaskId].Flag = 0;
    timer_10msDB[CompCtr_TaskId].Enable = TRUE;
    timer_10msDB[CompCtr_TaskId].Time = CompTaskCycle; // 500ms

    FanControlEvt();

    if (Flag3.OneBit.TestMode == 0) // 当前为测试模式
    {
        if (QuickFrozenState != quickFrozening) // 非速冻模式
        {
            if (ErrorState.OneBit.Ntc0Error != 0) // 柜温传感器故障
            {
                if (cfgPara[CoolingMode].sensorErrCompStopTime != 0)
                {
                    /*****传感器故障，压缩机逻辑*****/
                    SensorErrCompressorRun_Evt();
                    Reg_HeatState(OFF); // 打开除霜加热器
                }
                else // 取消比例开停
                {
                    // CompWorkState = Stopping;
                    Reg_CompressorState(REG_OFF); ////制冷停止中
                    Reg_HeatState(OFF);           // 打开除霜加热器
                }
            }
            else // 柜温传感器正常
            {
                switch (DefrostState)
                {
                case defrostingCycle: // 除霜周期内,正常运行逻辑判断
                {
                    /*****传感器正常，压缩机逻辑*****/
                    SensorNormalCompressorRun_Evt();
                    Reg_HeatState(OFF); // 打开除霜加热器
                }
                break;

                case defrosting: // 除霜中，除霜逻辑判断
                    // CompWorkState = Defrosting;
                    //  化霜温度传感器正常下逻辑
                    if (cfgPara[CoolingMode].defrosteType == 1) // 电加热化霜
                    {
                        Reg_CompressorState(OFF); // 关闭压缩机
                        Reg_HeatState(ON);        // 打开除霜加热器
                    }
                    else // 自然化霜方式
                    {
                        Reg_CompressorState(OFF); // 关闭压缩机
                        Reg_HeatState(OFF);       // 打开除霜加热器
                    }
                    break;

                case defrost_Dripping:
                    // CompWorkState = Defrost_Dripping;
                    Reg_CompressorState(OFF); // 关闭压缩机
                    Reg_HeatState(OFF);       // 关闭除霜加热器
                    break;                    // 滴水过程中
                default:
                    break;
                }
            }
        }
        else // 速冻模式
        {
            QuickFrozenModeCompRun_Evt();
            Reg_HeatState(OFF); // 关闭除霜加热器
        }
    }
}

/*********************************************************************
 * @fn      void DefrostStart_Evt(void)
 *
 * @brief   化霜开启
 *
 * @param   none
 *
 * @return  none
 */
void DefrostStart_Evt(void)
{
    tPara.defrosteCycleTimeCount = 0;
    tPara.defrosteTimeCount = 0;
    tPara.defrosteDripTimeCount = 0;

    DefrostState = defrosting;

    if (Flag3.OneBit.factoryMode == 0) // 进入工厂测试逻辑下
    {
        if (cfgPara[CoolingMode].defrosteShowMode == 1 || cfgPara[CoolingMode].defrosteShowMode == 2) // 模式2,3要假显示
        {
            Flag2.OneBit.PretendDisplay = 1; // 假显
        }
        else
        {
            Flag2.OneBit.PretendDisplay = 0; // 正常显示
        }
        BeforeDefrosteTemp = DisplayTemp; // 记录除霜前的柜温
    }
    Flag2.OneBit.Blink = DISPLAY;
}

/*********************************************************************
 * @fn      void DefrostStart_Evt(void)
 *
 * @brief   化霜开启
 *
 * @param   none
 *
 * @return  none
 */
void DefrostDripStart_Evt(void)
{
    tPara.defrosteCycleTimeCount = 0;
    tPara.defrosteTimeCount = 0;
    tPara.defrosteDripTimeCount = 0;

    DefrostState = defrost_Dripping; // 滴水过程中
}

/*********************************************************************
 * @fn      void DefrostStart_Evt(void)
 *
 * @brief   化霜开启
 *
 * @param   none
 *
 * @return  none
 */
void DefrostingCycleStart_Evt(void)
{
    tPara.defrosteCycleTimeCount = 0;
    tPara.defrosteTimeCount = 0;
    tPara.defrosteDripTimeCount = 0;

    DefrostState = defrostingCycle; // 除霜周期内

    if (Flag3.OneBit.factoryMode == 0) // 进入工厂测试逻辑下
    {
        if (QuickFrozenState == q_Ready)
        {
            QuickFrozenState = quickFrozening;
            tPara.quickFrozenTimeCount = 0;
        }

        Flag2.OneBit.Alarmd8Delay = 1; // 除霜后，如果有高温报警，不会立即产生高温报警，需要等待d8时间后才开始报警。(高温屏蔽)
        tPara.d8_Count = 0;
        Flag2.OneBit.AfterDefrosteFanRun = 1;
    }
}
/*********************************************************************
 * @fn      void CompressorDefrosteEvt(void)
 *
 * @brief   压缩机化霜检测事件
 *
 * @param   none
 *
 * @return  none
 */
void CompressorDefrosteDectEvt(void)
{
    timer_10msDB[CompDefsort_TaskId].Flag = 0;
    timer_10msDB[CompDefsort_TaskId].Enable = ENABLE;
    timer_10msDB[CompDefsort_TaskId].Time = CompTaskCycle; // 100ms

    CtrTargetTemp = cfgPara[CoolingMode].St * 10;

    if (Flag2.OneBit.RealDisplayDelay == 1)
    {
        tPara.Count5s++;
        if ((tPara.Count5s * CompTaskCycle) > (5 * Second))
        {
            tPara.Count5s = 0;
            Flag2.OneBit.RealDisplayDelay = 0;

            Flag3.OneBit.DisplayState = Real;
            tPara.Count2s = 0;
            tPara.Count30m = 0;
        }
    }

    if (Flag2.OneBit.AlarmPowerOnDelay == 0) // D8延时标志产生时处理  [D8=除霜后高温报警屏蔽时间]
    {
        tPara.AS_Count++;
        if ((tPara.AS_Count * CompTaskCycle) >= (cfgPara[CoolingMode].AS * tHour)) // 除霜结束后，测量温度等于或低于设定温度或经过d8时间后
        {
            tPara.AS_Count = 0;
            Flag2.OneBit.AlarmPowerOnDelay = 1;
        }
    }

    if (Flag3.OneBit.TestMode == 0) // 当前为测试模式
    {
        if (cfgPara[CoolingMode].defrosteCycleTime != 0) // 有除霜功能
        {
            switch (DefrostState)
            {
            case defrostingCycle: // 除霜间隔过程中DefrostState
            {
                if (Flag3.OneBit.factoryMode == 1) // 进入工厂测试逻辑下
                {
                    tPara.factoryModeTimeCount++;
                    if ((tPara.factoryModeTimeCount * CompTaskCycle) >= (cfgPara[CoolingMode].FactoryTestTime * tMinute)) // 温度低于设定值, 或者压缩机运行时间超过最大开机时间
                    {
                        tPara.factoryModeTimeCount = 0;
                        Flag2.OneBit.CompFirstRun = 0;

                        DefrostDripStart_Evt();
                        Flag3.OneBit.factoryModePart = 0; // 第一阶段
                    }
                }
                else
                {
                    if (Flag2.OneBit.Alarmd8Delay == 1) // D8延时标志产生时处理  [D8=除霜后高温报警屏蔽时间]
                    {
                        tPara.d8_Count++;
                        if ((tPara.d8_Count * CompTaskCycle) >= (cfgPara[CoolingMode].d8 * tMinute)) // 除霜结束后，测量温度等于或低于设定温度或经过d8时间后
                        {
                            tPara.d8_Count = 0;
                            Flag2.OneBit.Alarmd8Delay = 2;

                            Flag2.OneBit.PretendDisplay = 0;                                                           // 假显结束
                            Timer_Event_Set(&timer_10msDB[DisplayRefurbish_TaskId], 250, ENABLE, 0, DisplayRefurbish); // 250ms刷新
                        }
                    }

                    if (Flag2.OneBit.PretendDisplay == 1) // 退出除霜后假显示的逻辑
                    {
                        if (cfgPara[CoolingMode].defrosteShowMode == 1) // 除霜时显示进入除霜前的温度。
                        {
                            if (TempInside <= BeforeDefrosteTemp) // 温度小于进入化霜前的温度
                            {
                                TargetTemp = TempInside;
                                Flag2.OneBit.PretendDisplay = 0;                                                           // 假显结束
                                Timer_Event_Set(&timer_10msDB[DisplayRefurbish_TaskId], 250, ENABLE, 0, DisplayRefurbish); // 250ms刷新
                            }
                        }
                        else
                        {
                            if (TempInside <= (cfgPara[CoolingMode].St * 10)) // 温度小于设置温度
                            {
                                TargetTemp = TempInside;
                                Flag2.OneBit.PretendDisplay = 0;                                                           // 假显结束
                                Timer_Event_Set(&timer_10msDB[DisplayRefurbish_TaskId], 250, ENABLE, 0, DisplayRefurbish); // 250ms刷新
                            }
                        }
                    }

                    if (QuickFrozenState == quickFrozening) // 速冻模式下，压缩机累计时间判断
                    {
                        if ((tPara.defrosteCycleTimeCount * CompTaskCycle) <= (cfgPara[CoolingMode].defrosteCycleTime * tHour)) // 正常除霜周期内判断
                        {
                            if (CompressorState == REG_ON)
                            {
                                tPara.defrosteCycleTimeCount++;
                            }
                        }
                    }
                    else
                    {
                        if (CompressorState == REG_ON)
                        {
                            tPara.defrosteCycleTimeCount++;
                        }
                        if ((tPara.defrosteCycleTimeCount * CompTaskCycle) > (cfgPara[CoolingMode].defrosteCycleTime * tHour)) // 正常除霜周期内判断
                        {
                            DefrostStart_Evt();
                        }
                    }
                }
            }
            break;

            case defrosting: // 除霜过程中
            {
                // 化霜时间
                tPara.defrosteTimeCount++;
                if (Flag3.OneBit.factoryMode == 1) // 进入工厂测试逻辑下
                {
                    if ((tPara.defrosteTimeCount * CompTaskCycle) > (5 * tMinute)) // 第一阶段化霜结束后
                    {
                        DefrostDripStart_Evt();
                        Flag3.OneBit.factoryModePart = 1; // 化霜过后第二阶段测试
                    }
                }
                else
                {
                    // 正常状态下化霜逻辑
                    // tPara.defrosteTimeCount++;
                    if ((tPara.defrosteTimeCount * CompTaskCycle) > (cfgPara[CoolingMode].defrosteTime * tMinute))
                    {
                        /**** 化霜时间到,终止除霜进入滴水状态 ****/
                        DefrostDripStart_Evt();
                        break;
                    }

                    if (cfgPara[CoolingMode].defrosteType == 1)
                    {
                        if (ErrorState.OneBit.Ntc1Error == 0) // 化霜传感器正常
                        {
                            if (DefrostTemp >= (cfgPara[CoolingMode].defrosteEndTemp * 10)) // 除霜终止温度达到，结束除霜逻辑
                            {
                                /**** 化霜传感器 高于化霜终止温度,终止除霜进入滴水状态 ****/
                                DefrostDripStart_Evt();
                                break;
                            }
                        }
                    }
                }
            }
            break;

            case defrost_Dripping: // 滴水过程中
            {
                tPara.defrosteDripTimeCount++;
                if (Flag3.OneBit.factoryMode == 1) // 进入工厂测试逻辑下
                {
                    if (Flag3.OneBit.factoryModePart == 0) // 第一阶段停机后化霜开始
                    {
                        if ((tPara.defrosteDripTimeCount * CompTaskCycle) > (3 * tMinute))
                        {
                            DefrostStart_Evt();
                        }
                    }
                    else // 第二阶段，正常开始,退出测试模式
                    {
                        if ((tPara.defrosteDripTimeCount * CompTaskCycle) > (2 * tMinute)) // 化霜后滴水时间结束后，则退出测试模式
                        {
                            CoolingMode = L_CoolingMode;
                            Flag3.OneBit.factoryMode = 0;
                            Flag3.OneBit.factoryModePart = 0;
                            tPara.factoryModeTimeCount = 0;
                            DefrostingCycleStart_Evt();
                        }
                    }
                }
                else
                {
                    // 正常状态下滴水时间逻辑
                    // tPara.defrosteDripTimeCount++;
                    if ((tPara.defrosteDripTimeCount * CompTaskCycle) > (cfgPara[CoolingMode].defrosteDripTime * tMinute))
                    {
                        DefrostingCycleStart_Evt();
                    }
                }
            }
            break;

            default:
                break;
            }
        }
        else
        {
            DefrostState = defrostingCycle;
            tPara.defrosteCycleTimeCount = 0;
            tPara.defrosteTimeCount = 0;
            tPara.defrosteDripTimeCount = 0;
        }
        /********************** 压缩机运行时间统计 **********************/
        if (CompressorState == REG_ON)
        {
            tPara.CompressorRunTimeCount++;
            tPara.CompressorDownTimeCount = 0;
        }
        else // 压缩机停机时间统计
        {
            tPara.CompressorDownTimeCount++;
            tPara.CompressorRunTimeCount = 0;
        }
    }
}

/*********************************************************************
 * @fn      void FanControlEvt (void)
 *
 * @brief   风机机控制事件
 *
 * @param   none
 *
 * @return  none
 */
void FanControlEvt(void)
{
    if (Flag2.OneBit.FanPowerOnDelay == 0) // 风机首次上电延时
    {
        tPara.FanDelayCount++;
        if ((tPara.FanDelayCount * FanTaskCycle) >= (cfgPara[CoolingMode].fanDelayTime1 * tMinute))
        {
            Flag2.OneBit.FanPowerOnDelay = 1;
            tPara.FanDelayCount = 0;
        }
    }

    if (cfgPara[CoolingMode].fanMode != 0)
    {
        if (CompressorState == REG_ON) // F3 除霜后风机启动延时, 相当于每次压缩机开机，风机都需要延时
        {
            if (Flag2.OneBit.FanDefrosteDelay == 0)
            {
                tPara.FanDelay2Count++;
                if ((tPara.FanDelay2Count * FanTaskCycle) >= (cfgPara[CoolingMode].fanDelayTime2 * tMinute))
                {
                    Flag2.OneBit.FanDefrosteDelay = 1;
                    tPara.FanDelay2Count = 0;
                }
            }
        }
        else
        {
            Flag2.OneBit.FanDefrosteDelay = 0;
            tPara.FanDelayCount = 0;
        }

        if (Flag3.OneBit.TestMode == 0)
        {
            // if (Flag2.OneBit.FanPowerOnDelay == 1)
            // {
            //     switch (cfgPara[CoolingMode].fanMode)
            //     {
            //     case 0:
            //         if (DefrostState != defrostingCycle)
            //         {
            //             Reg_FanState(OFF);
            //         }
            //         else
            //         {
            //             if (CompressorState == REG_ON)
            //             {
            //                 if (Flag2.OneBit.AfterDefrosteFanRun == 1)
            //                 {
            //                     if (Flag2.OneBit.FanDefrosteDelay == 1)
            //                     {
            //                         Reg_FanState(ON);
            //                         Flag2.OneBit.AfterDefrosteFanRun = 0;
            //                     }
            //                     else
            //                     {
            //                         Reg_FanState(OFF);
            //                     }
            //                 }
            //                 else
            //                 {
            //                     Reg_FanState(OFF);
            //                 }
            //             }
            //             else
            //             {
            //                 Reg_FanState(OFF);
            //             }
            //         }
            //         break;

            //     case 1: //与制冷同启同停,化霜后风机延时输出

            //         if (CompressorState == REG_ON)
            //         {
            //             if (Flag2.OneBit.AfterDefrosteFanRun == 1)
            //             {
            //                 if (Flag2.OneBit.FanDefrosteDelay == 1)
            //                 {
            //                     Reg_FanState(ON);
            //                     Flag2.OneBit.AfterDefrosteFanRun = 0;
            //                 }
            //                 else
            //                 {
            //                     Reg_FanState(OFF);
            //                 }
            //             }
            //             else
            //             {
            //                 Reg_FanState(OFF);
            //             }
            //         }
            //         else
            //         {
            //             Reg_FanState(OFF);
            //         }

            //         break;

            //     default:
            //         break;
            //     }
            // }
            // else
            // {
            //     Reg_FanState(OFF);
            // }

            switch (cfgPara[CoolingMode].fanMode)
            {
            case 0:
                if (Flag2.OneBit.FanPowerOnDelay == 1)
                {
                    if (DefrostState != defrostingCycle)
                    {
                        Reg_FanState(ON);
                    }
                    else
                    {
                        if (Flag2.OneBit.AfterDefrosteFanRun == 1)
                        {
                            if (CompressorState == REG_ON && Flag2.OneBit.FanDefrosteDelay == 1)
                            {
                                Reg_FanState(ON);
                                Flag2.OneBit.AfterDefrosteFanRun = 0;
                            }
                            else
                            {
                                Reg_FanState(OFF);
                            }
                        }
                        else
                        {
                            if (CompressorState == REG_ON)
                            {
                                Reg_FanState(ON);
                            }
                            else
                            {
                                Reg_FanState(OFF);
                            }
                        }
                    }
                }
                else
                {
                    Reg_FanState(OFF);
                }

            case 1: // 与制冷同启同停,化霜后风机延时输出
                if (Flag2.OneBit.FanPowerOnDelay == 1)
                {
                    if (Flag2.OneBit.AfterDefrosteFanRun == 1)
                    {
                        if (CompressorState == REG_ON && Flag2.OneBit.FanDefrosteDelay == 1)
                        {
                            Reg_FanState(ON);
                            Flag2.OneBit.AfterDefrosteFanRun = 0;
                        }
                        else
                        {
                            Reg_FanState(OFF);
                        }
                    }
                    else
                    {
                        if (CompressorState == REG_ON)
                        {
                            Reg_FanState(ON);
                        }
                        else
                        {
                            Reg_FanState(OFF);
                        }
                    }
                }
                else
                {
                    Reg_FanState(OFF);
                }
                break;

            default:
                break;
            }
        }
    }
}

#ifdef FAN_PWM
/*********************************************************************
 * @fn      void FanControlEvt (void)
 *
 * @brief   风机机控制事件
 *
 * @param   none
 *
 * @return  none
 */
void FanPwm(uint8_t state)
{
    if (state == ON)
    {
        PWM0_Set_Duty(FanPwmList[cfgPara[CoolingMode].fanpwm]);
    }
    else
    {
        PWM0_Set_Duty(0);
    }
}
#endif